<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property integer $id
 * @property string $title
 * @property string $sapo
 * @property string $content
 * @property string $avatar
 * @property integer $status
 * @property string $date_created
 * @property string $date_modified
 */
class News extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return News the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'news';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, sapo, content', 'required'),
            array('id, status', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 500),
            array('avatar', 'length', 'max' => 255),
            array('date_created, date_modified', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, sapo, content, avatar, status, date_created, date_modified', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'title' => 'Tiêu đề',
            'sapo' => 'Sapo',
            'content' => 'Nội dung',
            'avatar' => 'Ảnh đại diện',
            'status' => 'Trạng thái',
            'date_created' => 'Ngày tạo',
            'date_modified' => 'Ngày sửa',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        //$criteria->order = 'news.id desc';
        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('sapo', $this->sapo, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('avatar', $this->avatar, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('date_created', $this->date_created, true);
        $criteria->compare('date_modified', $this->date_modified, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 'date_modified DESC',
            ),

        ));
    }

    /**
     * Danh sách các trạng thái có thể có của một banner
     * @return array
     */
    public function getListStatus()
    {
        return array(
            '0' => Yii::t('admin', 'Disabled'),
            '1' => Yii::t('admin', 'Enabled'),
        );
    }

    public function getStatusName($status)
    {
        $arr = $this->getListStatus();
        return $arr[$status];
    }
}